﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;

namespace FruitWeigher
{
	public partial class Weigher : UserControl
	{
		public Weigher()
		{
			// 为初始化变量所必需
			InitializeComponent();
		}
        public void CreateItemShow(ItemInfo? l1, ItemInfo? l2, ItemInfo? r3, ItemInfo? r4)
        {
            foreach (var item in WLeft.Children)
            {
                item.Visibility = Visibility.Collapsed;
                (item as UIItem0).Heft = 0;
            }
            foreach (var item in WRight.Children)
            {
                item.Visibility = Visibility.Collapsed;
                (item as UIItem0).Heft = 0;
            }
            if (l1 != null)
            {
                WLeft.Children[0].Visibility = Visibility.Visible;
                (WLeft.Children[0] as UIItem0).Type = l1.Value.Type;
                (WLeft.Children[0] as UIItem0).Heft = l1.Value.Heft;
            }
            if (l2 != null)
            {
                WLeft.Children[1].Visibility = Visibility.Visible;
                (WLeft.Children[1] as UIItem0).Type = l2.Value.Type;
                (WLeft.Children[1] as UIItem0).Heft = l2.Value.Heft;
            }
            if (r3 != null)
            {
                WRight.Children[0].Visibility = Visibility.Visible;
                (WRight.Children[0] as UIItem0).Type = r3.Value.Type;
                (WRight.Children[0] as UIItem0).Heft = r3.Value.Heft;
            }
            if (r4 != null)
            {
                WRight.Children[1].Visibility = Visibility.Visible;
                (WRight.Children[1] as UIItem0).Type = r4.Value.Type;
                (WRight.Children[1] as UIItem0).Heft = r4.Value.Heft;
            }
            NewMethod();
        }
        public void CreateItemShow(List<ItemInfo> list)
        {
            foreach (var item in WLeft.Children)
            {
                item.Visibility = Visibility.Collapsed;
                (item as UIItem0).Heft = 0;
            }
            foreach (var item in WRight.Children)
            {
                item.Visibility = Visibility.Collapsed;
                (item as UIItem0).Heft = 0;
            }
            if (list.Count == 2)
            {
                WLeft.Children[0].Visibility = Visibility.Visible;
                WRight.Children[0].Visibility = Visibility.Visible;
                (WLeft.Children[0] as UIItem0).Type = list[0].Type;
                (WLeft.Children[0] as UIItem0).Heft = list[0].Heft;
                (WRight.Children[0] as UIItem0).Type = list[1].Type;
                (WRight.Children[0] as UIItem0).Heft = list[1].Heft;
            }
            else
            {
                int r = GSystem.GamesRandom.Next() % list.Count;
                switch (list.Count)
                {
                    case 3:
                        if (GSystem.GamesRandom.Next() % 2 == 1)
                        {
                            WLeft.Children[0].Visibility = Visibility.Visible;
                            WRight.Children[0].Visibility = Visibility.Visible;
                            (WLeft.Children[0] as UIItem0).Type = list[0].Type;
                            (WLeft.Children[0] as UIItem0).Heft = list[0].Heft;
                            (WRight.Children[0] as UIItem0).Type = list[1].Type;
                            (WRight.Children[0] as UIItem0).Heft = list[1].Heft;
                        }
                        else
                        {

                        }
                        break;
                    case 4:
                        break;
                    case 5:
                        break;
                }
            }
            NewMethod();
            
        }

        private void NewMethod()
        {
            int Rheft = 0;
            int Lheft = 0;
            foreach (var item in WLeft.Children)
            {
                Lheft += (item as UIItem0).Heft;
            }
            foreach (var item in WRight.Children)
            {
                Rheft += (item as UIItem0).Heft;
            }
            if (Rheft == Lheft)
            {
                ANI_Def.Begin();
            }
            else
            {
                if (Rheft > Lheft)
                {
                    ANI_Right.Begin();
                }
                else
                {
                    ANI_Left.Begin();
                }
            }
        }
	}
}